<template>
  <el-drawer
    title="添加 / 编辑"
    size="50%"
    :wrapperClosable="false"
    :visible.sync="showModify"
  >
    <template slot="title">
      <span class="title">添加 / 编辑</span>
      <div class="btn-wrap">
        <el-button size="small" @click="close">关闭</el-button>
        <el-button type="primary" size="small" @click="save">提交</el-button>
      </div>
    </template>
    <div class="modify common-container">
      <div class="common-bot">
        <div class="modify-bot">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item required label="名称" style="width: 59%">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item required label="账号" style="width: 59%">
              <el-input v-model="form.mobile"></el-input>
            </el-form-item>
            <el-form-item required label="密码" style="width: 59%">
              <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item required label="性别" style="width: 59%">
              <el-select v-model="form.gender" placeholder="请选择">
                <el-option
                  v-for="item in genderOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch v-model="form.enable"></el-switch>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import Api from "@/api/";

export default {
  props: {
    showModify: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      default: "add",
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      form: {
        formId: 0,
        name: "",
        mobile: "",
        password: "",
        gender: "male",
        enable: true,
      },
      genderOptions: [
        {
          label: "男",
          value: "male",
        },
        {
          label: "女",
          value: "female",
        },
      ],
    };
  },
  watch: {
    async showModify(val) {
      if (val) {
        if (this.type == "edit") {
          this.getInfo();
        }
      }
    },
  },
  methods: {
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        formId: 0,
        name: "",
        mobile: "",
        password: "",
        gender: "male",
        enable: true,
      };
    },
    //
    getInfo() {
      Api.manager
        .detail({
          targetId: this.id,
        })
        .then((res) => {
          this.form = {
            formId: res.data.id,
            name: res.data.name,
            mobile: res.data.mobile,
            password: res.data.password,
            gender: res.data.gender,
            enable: res.data.enable,
          };
        });
    },
    checkInfo() {
      if (String(this.form.name).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写名称",
        });
        return false;
      }
      if (String(this.form.mobile).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写账号",
        });
        return false;
      }
      if (String(this.form.password).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写密码",
        });
        return false;
      }
      return true;
    },
    save() {
      if (!this.checkInfo()) {
        return;
      }
      Api.manager.modify(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.$emit("close");
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.modify {
  .modify-bot {
    width: 70%;
    padding: 30px 10px;
  }
}
</style>
